<?php

class CategoryController extends AdminController
{
    public function init()
    {
        $this->tableName = 'category';
        $this->typename = '分类';
		
        $this->assign('module', M('module')->select());
        parent::init();
    }

    public function indexAction()
    {
        $this->model->resetorderid();
        $list = $this->model->field('id,name,level,module_id,status,ordernum')->order('orderid asc')->select();
        foreach ($list as &$v) {
            --$v['level'];
        }
        $this->assign('list', $list);
        $this->assign('totalnum', count($this->list));
        $this->display();
    }

    public function delAction()
    {
        $id = I('post.id', 'int', 0);
        if ($id) {
            if (M('category')->where(array('pid'=>$id))->find()) {
                $this->error('当前栏目下含有子分类，请先删除子类！');
            } elseif (M('category')->where(array('id' => $id))->delete()) {
                $this->success('删除成功');
            }
        }
        $this->error('删除失败');
    }

    public function addAction()
    {
		$pid = I('get.pid', 'int', 0);
		$parent = array();
		if($pid){
			$parent = $this->model->find($pid);
			$parent['sonlevel'] = $parent['level']+1;
			$this->assign('parent', $parent);
		}else{
			$parent = array('pid'=>0,'level'=>1);
		}
        if (IS_POST) {
            $data = array(
                'name' => safetext($_POST['name']),
                'keywords' => safetext($_POST['keywords']),
                'description' => safetext($_POST['description']),
                'urlkey' => I('post.urlkey', 'en', ''),
                'module_id' => I('post.module_id', 'en', ''),
                'ordernum' => I('post.ordernum', 'int', 50),
                'status' => I('post.status', 'int', 1),
                'level' => I('post.level', 'int', 1),
                'pid' => I('post.status', 'int', 0),
            );
            if (!$data['urlkey'] || $this->model->where(array('urlkey' => $data['urlkey']))->find()) {
                $this->error('urlkey填写错误');
            }
            if (!$data['name']) {
                $this->error('分类名称填写错误');
            }
            $res = $this->model->insert($data);
            if ($res) {
                $this->model->resetorderid();
                $category_data = array();
                $category_data['category_id'] = $res;
                $category_data['viewnum'] = $data['viewnum'];
                $category_data['content'] = $data['content'];
                $category_data['datetime'] = time();
                M('page')->insert($category_data);
                $this->success('添加分类成功',U('admin.category.index'));
            } else {
                $this->error('添加分类失败');
            }
        }
        $this->actionname = '添加分类';
        $this->display();
    }

    public function editAction()
    {
        $id = I('request.id', 'int', 0);
        if (IS_POST) {
            $data = array(
                'name' => safetext($_POST['name']),
                'keywords' => safetext($_POST['keywords']),
                'description' => safetext($_POST['description']),
                'urlkey' => I('post.urlkey', 'en', ''),
                'thumb' => $_POST['thumb'],
                'module_id' => I('post.module_id', 'en', ''),
                'ordernum' => I('post.ordernum', 'int', '50'),
                'status' => I('post.status', 'int', '1'),
            );
            if (!$data['urlkey'] || $this->model->where("urlkey='{$data['urlkey']}' and id<>{$id}")->find()) {
                $this->error('urlkey填写错误');
            }
            if (!$data['name']) {
                $this->error('分类名称填写错误');
            }
            $res = $this->model->where(array('id'=>$id))->update($data);
            if ($res===0 or $res===1) {
                $this->model->resetorderid();
                $category_data = array();
                $category_data['viewnum'] = $_POST['viewnum'];
                $category_data['content'] = $_POST['content'];
                $category_data['template'] = $_POST['template'];
                $category_data['datetime'] = time();
                $page_res = M('page')->where(array('category_id'=>$id))->update($category_data);
                if($page_res == 0){ 
                    $category_data['category_id'] = $id;
                    M('page')->insert($category_data);
                }
                $this->success('修改分类成功',U('admin.category.index'));
            } else {
                $this->error('修改分类失败');
            }
        }
        $this->actionname='修改分类';
        $info = $this->model->where(array('id' => $id))->find();;
        $info_data=M('page')->where(array('category_id'=>$id))->find();

        //var_dump($info);
        $info['content'] = $info_data['content'];
        $info['viewnum'] = $info_data['viewnum'];
        $info['template'] = $info_data['template'];
        $this->info = $info;
        $this->display();
    }

    public function mulitAction()
    {
        foreach($_POST['name'] as $k=>$v){
            $data=array(
                'name'=>$_POST['name'][$k],
                'urlkey'=>$_POST['urlkey'][$k],
                'module'=>$_POST['module'][$k],
                'ordernum'=>$_POST['ordernum'][$k],
            );
            $this->model->where(array('id'=>$k))->update($data);
        }
        $this->model->resetorderid();
        $this->success('修改成功');
    }
}